package com.aptemo.webetek.client.widgets.games;

import java.util.ArrayList;
import java.util.Date;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.impl.GetPicksForEventAction;
import com.aptemo.webetek.client.commands.responses.GotListAsynchCallback;
import com.aptemo.webetek.client.enums.EnumEventStatus;
import com.aptemo.webetek.client.events.HasPickClickHandlers;
import com.aptemo.webetek.client.events.PickClickEvent;
import com.aptemo.webetek.client.events.PickClickHandler;
import com.aptemo.webetek.client.events.PickSelectedEvent;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.mvp.BaseDisplay;
import com.aptemo.webetek.client.mvp.BasePresenter;
import com.aptemo.webetek.shared.BetBean;
import com.aptemo.webetek.shared.EventBean;
import com.aptemo.webetek.shared.PickBean;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;

public class GameRowPresenter implements BasePresenter, ClickHandler
{

	public interface Display extends BaseDisplay
	{
		HasText getNameField();

		HasText getDescField();

		HasValue<Date> getDateField();

		void setBetPicksData(EventBean data, ClickHandler handler);

		String getClickedLink(ClickEvent event);

		// HasText getResultField();

		void setMyPicks(ArrayList<String> list);

		// HasClickHandlers getResultSubmit();
		//
		// HasText getResultCommentField();
		//
		// String getResultPickValue();

		HasPickClickHandlers getPickSelector();

		// HasClickHandlers getMyBetsLink();

	}

	private Display					display;
	private EventBean				data;
	private WebetekUserServiceAsync	rpcService;
	private HandlerManager			eventbus;

	// private List<StakeBean> stakeList;

	public GameRowPresenter(Display view, EventBean data, WebetekUserServiceAsync rpcService, HandlerManager eventbus)
	{
		this.display = view;
		this.data = data;
		this.rpcService = rpcService;
		this.eventbus = eventbus;
	}

	private void bind()
	{
		display.getPickSelector().addPickClickHandler(new PickClickHandler()
		{

			@Override
			public void onPickClicked(PickClickEvent event)
			{
				if (data.getStatus() == EnumEventStatus.ACTIVE)
				{
					PickSelectedEvent selev = new PickSelectedEvent(event.getPick());
					eventbus.fireEvent(selev);
				}
			}
		});

		// display.getMyBetsLink().addClickHandler(new ClickHandler()
		// {
		//
		// @Override
		// public void onClick(ClickEvent event)
		// {
		//
		// }
		// });

	}

	@Override
	public void go(HasWidgets container)
	{

		bind();

		display.getNameField().setText(data.getTitle());
		display.getDescField().setText(data.getDescription());
		display.getDateField().setValue(data.getDate());

		display.setBetPicksData(data, this);

		// load user stakes for this bet
		findStakes4Bet();
	}

	private void findStakes4Bet()
	{
		// TODO...
		GetPicksForEventAction action = new GetPicksForEventAction(data.getTeamId(), data.getKey());
		rpcService.invoke(action, new GotListAsynchCallback<String>()
		{

			@Override
			public void onSuccessList(ArrayList<String> result)
			{
				display.setMyPicks(result);			
			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				// TODO Auto-generated method stub
				
			}

		});

		// GetBetsAction action = new GetBetsAction(null);
		// action.setBetKey(data.getKey());
		// rpcService.invoke(action, new GotStakeListResponse()
		// {
		//
		// @Override
		// public void got(ArrayList<BetBean> list)
		// {
		// // renderList(list);
		// display.setMyPicks(list);
		// // stakeList = list;
		// }
		//
		// @Override
		// public void onError(String message, EErrorCodes code)
		// {
		// // display.getInfoField().setText("Error getting stakes: " +
		// // message);
		// }
		//
		// });

	}

	@Override
	public void onClick(ClickEvent event)
	{
		String pickname = display.getClickedLink(event);

		for (PickBean pick : data.getPicks())
		{
			if (pick.getTitle().equals(pickname))
			{
				eventbus.fireEvent(new PickSelectedEvent(pick));
				break;
			}
		}
		event.stopPropagation();

	}
}
